package com.ruoyi.file.controller;


import com.ruoyi.common.core.Entity.HttpStatusCode;
import com.ruoyi.common.core.Entity.MessageConstant;
import com.ruoyi.common.core.Entity.Result;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.file.FileUtils;
import com.ruoyi.file.service.ISysFileService;
import com.ruoyi.file.service.MinIOFileService;
import com.ruoyi.system.api.domain.SysFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Api(tags = "文件传输")
@RestController
public class FileController {

    @Autowired
    MinIOFileService minIOFileService;

    /**
     * 文件上传接口（支持多文件上传）
     * @param file 文件对象数组
     * @return 结果对象 (封装上传文件的预览文件路径)
     * @Author ChengLee
     * @Date 2022/11/25
     */
    @ApiOperation(value = "文件上传接口（支持多文件上传）")
    @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result upload(MultipartFile[] file) {
        if (file == null || file.length == 0){
            return new Result(HttpStatusCode.BAD_REQUEST, MessageConstant.UPLOAD_FILE_CAN_NOT_BE_NULL);
        }
        List<String> PreviewUrlList;
        try {
//            long start = System.currentTimeMillis();
            for (MultipartFile multipartFile : file) {
                if (multipartFile.getSize() > 6366888){ //单个文件大小限制 6.07M
                    return new Result(HttpStatusCode.WARNING,MessageConstant.FILE_TOO_LARGE);
                }
            }
            PreviewUrlList = minIOFileService.MinIOUpload(file);
//            long end = System.currentTimeMillis();
//            System.out.println("耗时：" + (end - start) / 1000 + "秒");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.UPLOAD_FILE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.UPLOAD_FILE_SUCCESS,PreviewUrlList);
    }

    /**
     * 文件下载接口
     * @param FileName 桶中的文件名
     * @return 结果对象
     */
    @ApiOperation(value = "文件下载接口（单文件）")
    @GetMapping("/download")
    public Result download(@RequestParam("FileName") String FileName, HttpServletResponse response){
        Result result;
        try {
            result = minIOFileService.MinIODownload(FileName, response);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.DOWNLOAD_FILE_FAIL);
        }
        return result;
    }

    /**
     * 获取文件预览 URL
     * @param FileName 要预览的文件名
     * @return 结果对象
     */
    @ApiOperation(value = "获取文件预览URL")
    @GetMapping("/preview")
    public Result getPreviewURL(@RequestParam("FileName")String FileName){
        Result result;
        try {
            result = minIOFileService.GetPreviewURL(FileName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.GET_PREVIEW_URL_FAIL);
        }
        return result;
    }

    /**
     * 删除指定文件
     * @param FineName 要删除的文件名
     * @return 结果对象
     */
    @ApiOperation(value = "删除文件")
    @DeleteMapping("/delete")
    public Result deleteFile(@RequestParam("FileName")String FineName){
        Result result;
        try {
            result = minIOFileService.deleteFile(FineName);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.DELETE_FILE_FAIL);
        }
        return result;
    }

    @Autowired
    private ISysFileService sysFileService;

    /**
     * 文件上传请求(原始)
     */
    @PostMapping("/oldupload")
    public R<SysFile> upload(MultipartFile file) {
        try {
            // 上传并返回访问地址
            String url = sysFileService.uploadFile(file);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            return R.ok(sysFile);
        }
        catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}